/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.web.demouser;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.feyaSoft.home.dao.base.BaseSearchResult;
import com.feyaSoft.home.hibernate.demouser.Demouser;
import com.feyaSoft.home.services.demouser.DemouserServiceImpl;
import com.feyaSoft.plugin.util.DateUtil;

/**
 * @author Fenqiang Zhuang
 * @Oct 5, 2007
 * 
 * This file is used to .....
 */
public class DemouserUtil {
	/**
	 * Set Demouser parameter - from user input
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static Demouser setDemouser(HttpServletRequest request,
			DemouserServiceImpl demouserService) throws Exception {
		Demouser demouser = new Demouser();

		if (request.getParameter("id") != null) {
			String id = (String) request.getParameter("id").trim();
			demouser = demouserService.getDemouserById(new Long(id));
		}
		if (request.getParameter("firstname") != null) {
			String firstname = (String) request.getParameter("firstname").trim();
			demouser.setFirstName(firstname);
		}

		if (request.getParameter("lastname") != null) {
			String lastname = (String) request.getParameter("lastname").trim();
			demouser.setLastName(lastname);
		}
		
		if (request.getParameter("username") != null) {
			String username = (String) request.getParameter("username").trim();
			demouser.setUsername(username);
		}
		
		if (request.getParameter("password") != null) {
			String password = (String) request.getParameter("password").trim();
			demouser.setPassword(password);
		}
		
		if (request.getParameter("note") != null) {
			String note = (String) request.getParameter("note").trim();
			demouser.setNote(note);
		}
		
		if (request.getParameter("email") != null) {
			String email = (String) request.getParameter("email").trim();
			demouser.setEmail(email);
		}

		if (request.getParameter("birthday") != null) {
			String birthdayString = (String) request.getParameter(
					"birthday").trim();
			// transfer format ...
			Date birthday = DateUtil.parse(birthdayString, "MMM dd, yyyy");
			demouser.setBirthday(birthday);
		}

		return demouser;
	}

	/**
	 * create JSon response out - for list of health page
	 * 
	 * @param request
	 * @param response
	 * @param tsts
	 * @throws Exception
	 */
	public static void jsonResponse(BaseSearchResult searchResult,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DemouserJson tsJson = new DemouserJson();
		List users = (List) searchResult.getResults();
		tsJson.setResults(users);
		tsJson.setTotalCount(searchResult.getTotalCount().intValue());

		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}
		response.getWriter().print(tsJson.toJSONObject());
		if (scriptTag) {
			out.write(");");
		}
	}
	
}
